var usernames;

$.getJSON("http://172.168.20.49/AiYeMall/php/aiye.php?key=?", "hide=getnames", data => {
    usernames = data;
})
$("#inp").focus(function () {
    this.style.color = "";
    sp.innerHTML = "";
    inp.style.borderColor = "#20c997";
})
inp.onblur =function (){
    if (inp.value == "") {
        sp.innerHTML = "用户名不能为空!";
        sp.style.color = "red";
        inp.style.borderColor = "red";
        return;
    }
    for (var i in usernames) {
        if (usernames[i]["user_name"] == this.value) {
            this.style.color = "red";
            sp.innerHTML = "此用户名已存在!";
            sp.style.color = "red";
            inp.style.borderColor = "red";
            return;
        }
    };
    sp.innerHTML = "用户名可用";
    sp.style.color = "green";
    inp.style.borderColor = "";
}


var inps = document.querySelectorAll("input");
inps[2].onfocus = function () {
    this.style.color = "";
    sp2.innerHTML = "";
    this.style.borderColor = "#20c997";
}
inps[2].onblur = function () {
    if (this.value == "") {
        sp2.innerHTML = "手机号不能为空!";
        sp2.style.color = "red";
        this.style.borderColor = "red";
        return;
    } else {
        var reg = /^(\+86|0086)?\s*1[3-9]\d{9}$/;
        console.log(this.value)
        var bool = reg.test(this.value);
        if (bool) {
            sp2.innerHTML = "手机号可用";
            sp2.style.color = "green";
            this.style.borderColor = "";
        } else {
            sp2.innerHTML = "手机号错误";
            sp2.style.color = "red";
            this.style.borderColor = "red";
        }
    }
}
inps[3].onfocus = function () {
    this.style.color = "";
    sp3.innerHTML = "";
    this.style.borderColor = "#20c997";
}
inps[3].onblur = function () {
    if (this.value == "") {
        sp3.innerHTML = "密码不能为空!";
        sp3.style.color = "red";
        this.style.borderColor = "red";
        return;
    }
    sp3.style.color = "green";
    this.style.borderColor = "";
}
inps[4].onfocus = function () {
    this.style.color = "";
    sp4.innerHTML = "";
    this.style.borderColor = "#20c997";
}
inps[4].onblur = function() {
    if (this.value == "") {
        sp4.innerHTML = "不能为空!";
        sp4.style.color = "red";
        this.style.borderColor = "red";
        return;
    }else if(this.value!=pwd.value){
        sp4.innerHTML="密码不同";
        sp4.style.color = "red";
        this.style.borderColor = "red";
        return;
    }
    sp4.style.color = "green";
    this.style.borderColor = "";
}


form.onsubmit = e => {
    inps[1].onblur();
    inps[2].onblur();
    inps[3].onblur();
    inps[4].onblur();
    var spans=document.querySelectorAll("#form span");
    for (var i = 1; i < spans.length; i++) {
        if (spans[i].style.color != "green") {
            return false;
        }
    }
}